<html>
<head><meta charset="utf-8"><title>representative example across all editions · edition 2021 · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/index.html">edition 2021</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html">representative example across all editions</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239184989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239184989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239184989">(May 18 2021 at 01:32)</a>:</h4>
<p>Is there a (short) piece of code that behaves differently in all three editions? For example, I was using this for 2015 / 2018 testing:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">async</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>It'd help me avoid some refactoring if there were a similar piece of code I could use for 2015/18/21.</p>



<a name="239185723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239185723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239185723">(May 18 2021 at 01:42)</a>:</h4>
<p>Untested:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;</span><span class="k">async</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">[</span><span class="mi">42</span><span class="p">].</span><span class="n">into_iter</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>EDIT: Oh, no, compilation doesn't get far enough to hit the 2021 difference.  Will need something more nuanced for 2015-2018.</p>



<a name="239204667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239204667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kestrer <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239204667">(May 18 2021 at 06:34)</a>:</h4>
<p>This code errors with different errors on 2015 and 2018, and should compile with 2021</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">async</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kt">i64</span>::<span class="n">try_from</span><span class="p">(</span><span class="mi">0_</span><span class="k">i32</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>And here's a polyglot that prints the edition number</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">mod</span> <span class="nn">m</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">func</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="mi">5</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="cp">#[allow(array_into_iter)]</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="fm">format!</span><span class="p">(</span><span class="s">"{:p}"</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="n">r</span><span class="p">].</span><span class="n">into_iter</span><span class="p">().</span><span class="n">next</span><span class="p">().</span><span class="n">unwrap</span><span class="p">())</span><span class="w"> </span><span class="o">==</span><span class="w">  </span><span class="fm">format!</span><span class="p">(</span><span class="s">"{:p}"</span><span class="p">,</span><span class="w"> </span><span class="n">r</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="fm">println!</span><span class="p">(</span><span class="s">"2021"</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="fm">println!</span><span class="p">(</span><span class="s">"2018"</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"2015"</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">m</span>::<span class="n">func</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="239230227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239230227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239230227">(May 18 2021 at 10:25)</a>:</h4>
<p>Haha, nice usage of 2015 namespacing rules (even in 2021 I still find it so confusing <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span>)</p>



<a name="239230375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239230375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239230375">(May 18 2021 at 10:27)</a>:</h4>
<p>Here is my attempt:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">MyFuture</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">r</span>#<span class="k">await</span>: <span class="err">…</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Future</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">MyFuture</span><span class="w"> </span><span class="err">…</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Future</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;'</span><span class="nb">_</span><span class="w"> </span><span class="n">MyFuture</span><span class="w"> </span><span class="err">…</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">r</span>#<span class="k">async</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="k">for</span><span class="w"> </span><span class="n">my_fut</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">[</span><span class="n">MyFuture</span>::<span class="n">default</span><span class="p">()].</span><span class="n">into_iter</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_it</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">futures</span>::<span class="n">future</span>::<span class="n">ready</span><span class="p">(());</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">_it</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">async</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">core</span>::<span class="n">any</span>::<span class="n">type_name_of_val</span><span class="p">(</span><span class="o">&amp;</span><span class="n">my_fut</span><span class="p">.</span><span class="k">await</span><span class="p">));</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">futures</span>::<span class="n">executor</span>::<span class="n">block_on</span><span class="p">(</span><span class="n">_it</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<ul>
<li><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=a2d59b876fb5fa775465a16848cf0165">Playground</a></li>
</ul>



<a name="239230918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239230918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239230918">(May 18 2021 at 10:32)</a>:</h4>
<p>(Maybe not the shortest example, but that's because I've wanted to feature type-distinct dispatch for that; I'd say that the</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">r</span>#<span class="k">async</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">async</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cm">/* … 1 … */</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cm">/* … 2 … */</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>is the most key thing to get code that compiles for both 2015 and 2018 and where 2018 and onwards, <code>…1…</code> is a lazy block; and there with a bit of <code>[…].into_iter()</code> we get a difference between 2018 and 2021 that does not involve prelude changes <em>per se</em>)</p>



<a name="239259631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239259631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239259631">(May 18 2021 at 14:08)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> <code>panic!(async {})</code></p>



<a name="239468618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239468618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239468618">(May 19 2021 at 17:54)</a>:</h4>
<p>As a <em>thing</em>, this seems to relatively consistently time out my tests in GitHub Actions.</p>



<a name="239468645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239468645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239468645">(May 19 2021 at 17:54)</a>:</h4>
<p>Gonna increase my timeouts from 5 seconds to 10</p>



<a name="239468667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239468667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239468667">(May 19 2021 at 17:54)</a>:</h4>
<p>And the playground gets ever slower.</p>



<a name="239470557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239470557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239470557">(May 19 2021 at 18:06)</a>:</h4>
<p>ah, I've been noticing the playground timing out a lot recently, is it backed by github actions these days? there's no way rustc is taking five seconds to compile such a trivial program, surely</p>



<a name="239473968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239473968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239473968">(May 19 2021 at 18:28)</a>:</h4>
<p>Continued in <a href="#narrow/stream/242791-t-infra/topic/playground.20oddly.20slow">https://rust-lang.zulipchat.com/#narrow/stream/242791-t-infra/topic/playground.20oddly.20slow</a></p>



<a name="239484994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239484994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239484994">(May 19 2021 at 19:46)</a>:</h4>
<p>Ah, switching editions appears to trigger a rebuild. That's exciting. I'll open a new topic.</p>



<a name="239485137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/268952-edition%202021/topic/representative%20example%20across%20all%20editions/near/239485137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/268952-edition-2021/topic/representative.20example.20across.20all.20editions.html#239485137">(May 19 2021 at 19:47)</a>:</h4>
<div class="codehilite"><pre><span></span><code>real    1m8.700s
user    3m43.132s
</code></pre></div>
<p>yep, that'll blow out the 10 second timeout</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>